package com.itheima.service.umsService.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.db.ums.*;
import com.itheima.mapper.umsMapper.*;
import com.itheima.service.umsService.UmsRoleService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DogeGuo
 */
@DubboService
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements UmsRoleService {
    @Autowired
    private UmsRoleMapper umsRoleMapper;

    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Autowired
    private UmsResourceMapper umsResourceMapper;

    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    //根据角色id集合查询角色集合
    @Override
    public List<UmsRole> findByRoleIds(List<Long> roleIds) {
        List<UmsRole> roleList = umsRoleMapper.selectBatchIds(roleIds);
        return roleList;
    }

    @Override
    public PageBeanVo findAll(Integer pageNum, Integer pageSize,String keyword) {
        //1.设置分页参数
        Page<UmsRole> page = new Page<>(pageNum, pageSize);

        //2.创建条件查询对象-userId
        LambdaQueryWrapper<UmsRole> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(keyword),UmsRole::getName, keyword);

        //3.调用mapper.selectPage(..)
        umsRoleMapper.selectPage(page,qw);

        //4.封装pageBeanVo返回
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    /*
     * 查询所有角色
     * */
    @Override
    public List<UmsRole> findAllRoles() {
        LambdaQueryWrapper<UmsRole> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsRole::getStatus,1);
        return umsRoleMapper.selectList(qw);
    }


    // 根据角色名称查询角色
    @Override
    public UmsRole findRoleByName(String name) {
        LambdaQueryWrapper<UmsRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRole::getName, name);
        return umsRoleMapper.selectOne(queryWrapper);
    }


    // 创建角色
    @Override
    public void creatRole(UmsRole umsRole) {
        umsRoleMapper.insert(umsRole);
    }

    @Override
    public UmsRole findById(Integer roleId) {
        return umsRoleMapper.selectById(roleId);
    }

    // 更新角色的信息
    @Override
    public void updateRoleInfo(UmsRole umsRole) {
        umsRoleMapper.updateById(umsRole);
    }

    // 批量删除角色
    @Override
    public void deleteRoleByIds(List<Long> roleIds) {
        // 删除角色的话 要删除角色 和 资源菜单关系的中间表信息
        // 1 删除角色和菜单关系表中的数据
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UmsRoleMenuRelation::getRoleId, roleIds);
        umsRoleMenuRelationMapper.delete(queryWrapper);
        // 2 删除角色和资源关系中间表的数据
        LambdaQueryWrapper<UmsRoleResourceRelation> qw = new LambdaQueryWrapper<>();
        qw.in(UmsRoleResourceRelation::getRoleId, roleIds);
        umsRoleResourceRelationMapper.delete(qw);

        // 3 最后删除角色信息
        LambdaQueryWrapper<UmsRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UmsRole::getId, roleIds);
        umsRoleMapper.delete(lambdaQueryWrapper);
    }

    //获取角色相关菜单
    @Override
    public List<UmsMenu> findRoleMenu(Long roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        // 先查询角色菜单关系表
        List<UmsRoleMenuRelation> roleMenuRelations = umsRoleMenuRelationMapper.selectList(queryWrapper);
        // 遍历集合
        if (CollUtil.isNotEmpty(roleMenuRelations)) {
            List<Long> menuIds = roleMenuRelations.stream().map(UmsRoleMenuRelation::getMenuId).collect(Collectors.toList());
            List<UmsMenu> umsMenus = umsMenuMapper.selectBatchIds(menuIds);
            return umsMenus;
        }
        return null;
    }

    // 给角色分配菜单
    @Override
    public void roleAllocMenu(Long roleId, List<Long> menuIds) {
        // 先将原有的角色菜单关系表给删除
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        umsRoleMenuRelationMapper.delete(queryWrapper);
        // 将新的菜单表和角色关联起来存入
        if (CollUtil.isNotEmpty(menuIds)) {
            for (Long menuId : menuIds) {
                UmsRoleMenuRelation umsRoleMenuRelation = new UmsRoleMenuRelation();
                umsRoleMenuRelation.setRoleId(roleId);
                umsRoleMenuRelation.setMenuId(menuId);
                umsRoleMenuRelationMapper.insert(umsRoleMenuRelation);
            }
        }
    }

    //获取指定角色角色相关资源
    @Override
    public List<UmsResource> findRoleResourceListByRoleId(Long roleId) {
        List<Long> resourceIdList = umsRoleResourceRelationMapper.findRoleResourceListByRoleId(roleId);
        return umsResourceMapper.selectBatchIds(resourceIdList);
    }

    //给指定角色分配资源
    @Override
    public void roleAllocResource(Long roleId, List<Long> resourceIds) {
        // 先将原有的角色资源关系表中的数据删除
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId, roleId);
        umsRoleResourceRelationMapper.delete(queryWrapper);
        // 将新的资源id 构建关系对象 加入关系表中
        if (CollUtil.isNotEmpty(resourceIds)) {
            for (Long resourceId : resourceIds) {
                UmsRoleResourceRelation roleResourceRelation = new UmsRoleResourceRelation();
                roleResourceRelation.setRoleId(roleId);
                roleResourceRelation.setResourceId(resourceId);
                // 插入到关系表中
                umsRoleResourceRelationMapper.insert(roleResourceRelation);
            }
        }
    }
}
